unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ScrollBar1: TScrollBar;
    Edit1: TEdit;
    Timer1: TTimer;
    Timer2: TTimer;
    ScrollBar2: TScrollBar;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  u, n, i, j, z, t, d : integer;
  arr: array of TPanel;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (u = 1) then
    for i:= 0 to n-1 do
    begin
      arr[i].Destroy;
    end;
  n:=StrToInt (Edit1.Text);
  setlength (arr, n);
  randomize;
  for i:= 0 to n-1 do
    begin
      arr[i]:= TPanel.Create(Form1);
      arr[i].Parent:= Form1;
      arr[i].Width := 30;
      arr[i].Height := 30;
      arr[i].Top := 40;
      arr[i].Left := 50*i;
      arr[i].Caption := IntToStr(Random (50)+1);
      arr[i].Font.Color:= clBlack;
    end;
  u:=1;
  if (n=1) then
    arr[0].Color:= clWhite;
  if (n>1) then
  begin
    i:= 0;
    j:= 0;
    Timer1.Interval:=Scrollbar1.Position;
    Timer1.Enabled := true;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var g, k: integer;
begin
  for g:= 0 to n-1-j do
  begin
    arr[g].Color:= cl3DLight;
  end;
  k:=0;
  Timer1.Interval:=Scrollbar1.Position;
  if (i = n-1-j) then
  begin
    i:= 0;
    j:=j+1;
  end;
  arr[i].Color:= clRed;
  arr[i+1].Color:= clRed;
  t:= StrToInt(arr[i].Caption);
  d:= StrToInt(arr[i+1].Caption);
  if (t > d) then
    begin
      z:=arr[i+1].Left-arr[i].Left;
      Timer2.Interval:=Scrollbar2.Position;
      Timer2.Enabled:=true;
      Timer1.Enabled:=false;
      exit;
      //arr[i].Caption:= IntToStr(d);
      //arr[i+1].Caption:= IntToStr(t);
    end;
  i:= i+1;
  for g:= 0 to n-2 do
  begin
    t:= StrToInt(arr[g].Caption);
    d:= StrToInt(arr[g+1].Caption);
    if (t > d) then
    begin
      k:=1;
    end;
  end;
  if (j<>0) then
    for g:= n-j to n-1 do
      arr[g].Color:= clWhite;
  if (k=0) then
  begin
   for g:= 0 to n-1 do
    begin
      arr[g].Color:= clWhite;
      arr[g].Top := 40;
      arr[g].Left := 50*g;
    end;
    Timer1.Enabled := false;
  end;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  Timer2.Interval:=Scrollbar2.Position;
  if (arr[i].Left- arr[i+1].Left < z) then
  begin
    arr[i].Left:= arr[i].Left+1;
    arr[i+1].Left:= arr[i+1].Left-1;
  end;
  if (arr[i].Left- arr[i+1].Left >= z) then
  begin
    t:= StrToInt(arr[i].Caption);
    d:= StrToInt(arr[i+1].Caption);
    arr[i].Caption:= IntToStr(d);
    arr[i+1].Caption:= IntToStr(t);
    arr[i].Left:=50*i;
    arr[i+1].Left:=50*(i+1);
    Timer1.Interval:=Scrollbar1.Position;
    Timer1.Enabled:=true;
    Timer2.Enabled:=false;
  end;
end;
end.
